"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--磁盘
Case Name   : 备节点重建，主节点磁盘IOwait高
Create At   : 2021/07/13
@zou_jialiang0505328126
Description :
    1.主节点注入IOwait高
    2.备节点重建
Expect      :
    1.成功
    2.成功
History     :
    2021/9/1 @wan005:适配最新CFE故障打印
"""

import unittest
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger


class CPUOverloadAndRunTPCC(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case048:开始执行---------')
        self.pri_root_node = Node(node='PrimaryRoot')
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()

        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.pri_root_node.sh(cmd).result()
        self.log.info(msg)
        self.dir = msg.splitlines()[-1].split()[-1].strip()
        self.hostmsg = self.pri_root_node.sh('hostname;').result()
        self.log.info(f'hostname is {self.hostmsg}')

    def test_main(self):
        text = '----step1: 主节点磁盘IOwait高 expect:成功----'
        self.log.info(text)
        inject_command = f"rIO_iowait (type,dir,bs,count,time) " \
            f"values (r_iowait,{self.dir}, 10000,10000,1000)"
        result = self.com.cfe_inject(self.pri_root_node, inject_command)
        self.log.info(result)
        result = self.com.cfe_query(self.pri_root_node, "rIO_iowait")
        self.log.info(result)
        self.assertIn(self.constant.cfe_iowait_success_msg, result,
                      '执行失败:' + text)

        text = '----step2: 备节点重建 expect:成功----'
        self.log.info(text)
        result = self.sta1_sh.executeGsctl('build',
                                          self.constant.BUILD_SUCCESS_MSG)
        self.assertTrue(result, '执行失败:' + text)

    def tearDown(self):
        self.log.info("--------------主节点清理故障-------------")
        result = self.com.cfe_clean(self.pri_root_node, "rIO_iowait")
        self.log.info(result)
        self.sta1_sh.executeGsctl('build',
                                  self.constant.BUILD_SUCCESS_MSG)
        self.log.info('-----Opengauss_Reliability_Hardware_Case048:执行完成-----')
